package reese.think_in_java.java_21_thread;

import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * ***********************************************************
 *
 * @类名 : reese.think_in_java.java_21_thread.类名
 * @DESCRIPTION : 这家伙太懒啥都没写
 * @AUTHOR :  一个风情万种的男子
 * @DATE : 2016/6/2
 * ***********************************************************
 */

class Accessor implements Runnable{
    private final int id ;

    public Accessor(int id) {
        this.id = id;
    }

    @Override
    public void run() {
       while (!Thread.currentThread().isInterrupted()){
           ThreadLocalVariableHolder.increment();
           System.out.println(this);
           Thread.yield();
       }
    }

    @Override
    public String toString() {
        return "Accessor{id}"+id+":" + ThreadLocalVariableHolder.get();
    }
}


public class ThreadLocalVariableHolder {

    private static ThreadLocal<Integer> value = new ThreadLocal<Integer>(){
        private Random rand = new Random(47);
        protected synchronized  Integer initialValue(){
            return rand.nextInt(10000);
        }
    };

    public static void increment() {
        value.set(value.get()+1);

    }

    public static int get() {
        return value.get();
    }

    public static void main(String[] args) throws InterruptedException {
        ExecutorService executorService = Executors.newCachedThreadPool();

        for (int i=0;i<5;i++){
            executorService.execute(new Accessor(i));
        }
        TimeUnit.SECONDS.sleep(3);
        executorService.shutdownNow();
    }
}
